package com.yunke.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunke.common.result.YunkeResult;
import com.yunke.entity.YunkeBanner;
import com.yunke.entity.YunkeHomeModule;
import com.yunke.service.YunkeBannerService;
import com.yunke.service.YunkeHomeModuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 首页管理员控制器
 * 
 * @author yunke
 * @since 1.0.0
 */
@RestController
@RequestMapping("/admin/home")
@Tag(name = "首页管理(管理员)", description = "首页管理员接口")
@SaCheckLogin
@SaCheckRole("admin")
public class YunkeHomeAdminController {

    @Autowired
    private YunkeHomeModuleService homeModuleService;

    @Autowired
    private YunkeBannerService bannerService;

    // ================== 首页模块管理 ==================

    @PostMapping("/modules")
    @Operation(summary = "创建首页模块")
    public YunkeResult<Boolean> createModule(@Valid @RequestBody YunkeHomeModule module) {
        boolean result = homeModuleService.createModule(module);
        return YunkeResult.success(result);
    }

    @PutMapping("/modules")
    @Operation(summary = "更新首页模块")
    public YunkeResult<Boolean> updateModule(@Valid @RequestBody YunkeHomeModule module) {
        boolean result = homeModuleService.updateModule(module);
        return YunkeResult.success(result);
    }

    @DeleteMapping("/modules/{id}")
    @Operation(summary = "删除首页模块")
    public YunkeResult<Boolean> deleteModule(
            @Parameter(description = "模块ID") @PathVariable Long id) {
        
        boolean result = homeModuleService.deleteModule(id);
        return YunkeResult.success(result);
    }

    @DeleteMapping("/modules/batch")
    @Operation(summary = "批量删除首页模块")
    public YunkeResult<Boolean> deleteModules(
            @Parameter(description = "模块ID列表") @RequestBody List<Long> ids) {
        
        boolean result = homeModuleService.deleteModules(ids);
        return YunkeResult.success(result);
    }

    @GetMapping("/modules/page")
    @Operation(summary = "分页查询模块列表")
    public YunkeResult<IPage<YunkeHomeModule>> getModulePage(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "模块类型") @RequestParam(required = false) Integer moduleType,
            @Parameter(description = "显示位置") @RequestParam(required = false) String displayPosition,
            @Parameter(description = "模块名称") @RequestParam(required = false) String moduleName,
            @Parameter(description = "是否启用") @RequestParam(required = false) Boolean isEnabled) {
        
        IPage<YunkeHomeModule> result = homeModuleService.getModulePage(current, size, 
            moduleType, displayPosition, moduleName, isEnabled);
        return YunkeResult.success(result);
    }

    @GetMapping("/modules/{id}")
    @Operation(summary = "根据ID查询模块详情")
    public YunkeResult<YunkeHomeModule> getModuleById(
            @Parameter(description = "模块ID") @PathVariable Long id) {
        
        YunkeHomeModule module = homeModuleService.getModuleById(id);
        return YunkeResult.success(module);
    }

    @PutMapping("/modules/{id}/enable")
    @Operation(summary = "启用模块")
    public YunkeResult<Boolean> enableModule(
            @Parameter(description = "模块ID") @PathVariable Long id) {
        
        boolean result = homeModuleService.enableModule(id);
        return YunkeResult.success(result);
    }

    @PutMapping("/modules/{id}/disable")
    @Operation(summary = "禁用模块")
    public YunkeResult<Boolean> disableModule(
            @Parameter(description = "模块ID") @PathVariable Long id) {
        
        boolean result = homeModuleService.disableModule(id);
        return YunkeResult.success(result);
    }

    @PutMapping("/modules/{id}/top")
    @Operation(summary = "置顶模块")
    public YunkeResult<Boolean> topModule(
            @Parameter(description = "模块ID") @PathVariable Long id) {
        
        boolean result = homeModuleService.topModule(id);
        return YunkeResult.success(result);
    }

    @PutMapping("/modules/{id}/untop")
    @Operation(summary = "取消置顶模块")
    public YunkeResult<Boolean> untopModule(
            @Parameter(description = "模块ID") @PathVariable Long id) {
        
        boolean result = homeModuleService.untopModule(id);
        return YunkeResult.success(result);
    }

    @PutMapping("/modules/enabled")
    @Operation(summary = "批量更新模块启用状态")
    public YunkeResult<Boolean> updateModuleEnabled(
            @Parameter(description = "模块ID列表") @RequestParam List<Long> ids,
            @Parameter(description = "启用状态") @RequestParam Boolean isEnabled) {
        
        boolean result = homeModuleService.updateModuleEnabled(ids, isEnabled);
        return YunkeResult.success(result);
    }

    // ================== 轮播图管理 ==================

    @PostMapping("/banners")
    @Operation(summary = "创建轮播图")
    public YunkeResult<Boolean> createBanner(@Valid @RequestBody YunkeBanner banner) {
        boolean result = bannerService.createBanner(banner);
        return YunkeResult.success(result);
    }

    @PutMapping("/banners")
    @Operation(summary = "更新轮播图")
    public YunkeResult<Boolean> updateBanner(@Valid @RequestBody YunkeBanner banner) {
        boolean result = bannerService.updateBanner(banner);
        return YunkeResult.success(result);
    }

    @DeleteMapping("/banners/{id}")
    @Operation(summary = "删除轮播图")
    public YunkeResult<Boolean> deleteBanner(
            @Parameter(description = "轮播图ID") @PathVariable Long id) {
        
        boolean result = bannerService.deleteBanner(id);
        return YunkeResult.success(result);
    }

    @DeleteMapping("/banners/batch")
    @Operation(summary = "批量删除轮播图")
    public YunkeResult<Boolean> deleteBanners(
            @Parameter(description = "轮播图ID列表") @RequestBody List<Long> ids) {
        
        boolean result = bannerService.deleteBanners(ids);
        return YunkeResult.success(result);
    }

    @GetMapping("/banners/page")
    @Operation(summary = "分页查询轮播图列表")
    public YunkeResult<IPage<YunkeBanner>> getBannerPage(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "轮播图类型") @RequestParam(required = false) Integer bannerType,
            @Parameter(description = "显示位置") @RequestParam(required = false) String displayPosition,
            @Parameter(description = "轮播图标题") @RequestParam(required = false) String bannerTitle,
            @Parameter(description = "是否启用") @RequestParam(required = false) Boolean isEnabled) {
        
        IPage<YunkeBanner> result = bannerService.getBannerPage(current, size, 
            bannerType, displayPosition, bannerTitle, isEnabled);
        return YunkeResult.success(result);
    }

    @GetMapping("/banners/{id}")
    @Operation(summary = "根据ID查询轮播图详情")
    public YunkeResult<YunkeBanner> getBannerById(
            @Parameter(description = "轮播图ID") @PathVariable Long id) {
        
        YunkeBanner banner = bannerService.getBannerById(id);
        return YunkeResult.success(banner);
    }

    @PutMapping("/banners/{id}/enable")
    @Operation(summary = "启用轮播图")
    public YunkeResult<Boolean> enableBanner(
            @Parameter(description = "轮播图ID") @PathVariable Long id) {
        
        boolean result = bannerService.enableBanner(id);
        return YunkeResult.success(result);
    }

    @PutMapping("/banners/{id}/disable")
    @Operation(summary = "禁用轮播图")
    public YunkeResult<Boolean> disableBanner(
            @Parameter(description = "轮播图ID") @PathVariable Long id) {
        
        boolean result = bannerService.disableBanner(id);
        return YunkeResult.success(result);
    }

    @PutMapping("/banners/enabled")
    @Operation(summary = "批量更新轮播图启用状态")
    public YunkeResult<Boolean> updateBannerEnabled(
            @Parameter(description = "轮播图ID列表") @RequestParam List<Long> ids,
            @Parameter(description = "启用状态") @RequestParam Boolean isEnabled) {
        
        boolean result = bannerService.updateBannerEnabled(ids, isEnabled);
        return YunkeResult.success(result);
    }

    @GetMapping("/banners/expired")
    @Operation(summary = "查询过期轮播图")
    public YunkeResult<List<YunkeBanner>> getExpiredBanners() {
        List<YunkeBanner> banners = bannerService.getExpiredBanners();
        return YunkeResult.success(banners);
    }

    @PutMapping("/banners/disable-expired")
    @Operation(summary = "自动禁用过期轮播图")
    public YunkeResult<Boolean> disableExpiredBanners() {
        boolean result = bannerService.disableExpiredBanners();
        return YunkeResult.success(result);
    }
} 